package com.ruoyi.web.controller.construction;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.construction.ConstructionInfo;
import com.ruoyi.system.domain.construction.Team;
import com.ruoyi.system.service.construction.ConstructionInfoService;
import com.ruoyi.system.service.construction.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 施工队信息表
 *
 * @author 张少丛
 */
@RestController
@RequestMapping("/construction/info")
public class ConstructionInfoController extends BaseController
{

    @Autowired(required = false)
    private ConstructionInfoService constructionInfoService;

    /**
     * 获取施工队信息列表
     */
    @PreAuthorize("@ss.hasPermi('construction:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(ConstructionInfo info){
        // 开启分页
        startPage();
        // 多条件查询施工队信息
        List<ConstructionInfo> infoList = constructionInfoService.selectConstructionInfoList(info);
        return getDataTable(infoList);
    }

    @GetMapping("/pingfenList")
    public TableDataInfo findAllListPingfen(ConstructionInfo constructionInfo){
        startPage();
        List<ConstructionInfo> allListPingfen = constructionInfoService.findAllListPingfen(constructionInfo);
        return getDataTable(allListPingfen);
    }

    @GetMapping(value = "pingfenById/{constructionId}")
    public AjaxResult pingfenById(@PathVariable("constructionId") Long constructionId)
    {
        return AjaxResult.success(constructionInfoService.caseInfoById(constructionId));
    }

    @PutMapping(value = "/pingfenEdit")
    public AjaxResult edit(@RequestBody ConstructionInfo constructionInfo)
    {
        return toAjax(constructionInfoService.updateConstructionInfo(constructionInfo));
    }


    /**
     * 根据编号获取详细信息
     * @param infoId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('construction:info:query')")
    @GetMapping(value = { "/", "/{infoId}" })
    public AjaxResult getInfo(@PathVariable(value = "infoId", required = false) Long infoId){
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(infoId))
        {
            ajax.put(AjaxResult.DATA_TAG, constructionInfoService.selectConstructionInfoById(infoId));
        }
        return ajax;
    }


    /**
     * 新增人员信息
     * @param constructionInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('construction:info:add')")
    @Log(title = "施工队人员管理", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult add(@Validated @RequestBody ConstructionInfo constructionInfo){

        if (UserConstants.NOT_UNIQUE.equals(constructionInfoService.checkConstructionInfoByName(constructionInfo)))
        {
            return AjaxResult.error("新增人员'" + constructionInfo.getConstructionName() + "'失败，人员信息已存在");
        }
        return AjaxResult.success(constructionInfoService.insertConstructionInfo(constructionInfo));
    }

    /**
     * 修改人员信息
     * @param constructionInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('construction:info:update')")
    @Log(title = "施工队人员管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult update(@Validated @RequestBody ConstructionInfo constructionInfo){
        if (UserConstants.NOT_UNIQUE.equals(constructionInfoService.checkConstructionInfoByName(constructionInfo)))
        {
            return AjaxResult.error("修改人员'" + constructionInfo.getConstructionName() + "'失败，人员信息已存在");
        }
        return AjaxResult.success(constructionInfoService.updateConstructionInfo(constructionInfo));
    }

    /**
     * 删除人员
     */
    @PreAuthorize("@ss.hasPermi('construction:info:remove')")
    @Log(title = "施工队人员管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{constructionIds}")
    public AjaxResult remove(@PathVariable Long[] constructionIds) {
        return toAjax(constructionInfoService.deleteConstructionInfoByIds(constructionIds));
    }

    /**
     * 状态修改
     */
//    @PreAuthorize("@ss.hasPermi('construction:info:edit')")
    @Log(title = "施工队人员管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody ConstructionInfo constructionInfo) {
        return toAjax(constructionInfoService.updateInfoStatus(constructionInfo));
    }

    /**
     * 修改人员审批状态
     */
    @PreAuthorize("@ss.hasPermi('construction:info:edit')")
    @Log(title = "施工队人员管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeAuditStatus")
    public AjaxResult changeAuditStatus(@RequestBody ConstructionInfo constructionInfo) {
        return toAjax(constructionInfoService.updateInfoAuditStatus(constructionInfo));
    }

    /**
     * 导出人员信息
     * @return
     */
    @PreAuthorize("@ss.hasPermi('construction:info:export')")
    @Log(title = "施工队人员管理", businessType = BusinessType.EXPORT)
    @RequestMapping("/export")
    public AjaxResult export(ConstructionInfo instructionInfo){
        List<ConstructionInfo> infoList = this.constructionInfoService.selectConstructionInfoList(instructionInfo);
        ExcelUtil<ConstructionInfo> util = new ExcelUtil<ConstructionInfo>(ConstructionInfo.class);
        return util.exportExcel(infoList, "施工队人员数据");
    }
}
